﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using FX.CC.Andon.Entity.DataManage;
using FX.CC.Andon.Entity.ReportManage;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.DataBase;

namespace FX.CC.Andon.Service.DataManage
{
    public class UnReliefAbnormalService : DbHelperBase
    {
        public int GetUnReliefAbnormalCount(AbnormalQcEntity qcmodel)
        {
            var count = 0;
            var strSql = new StringBuilder();
            strSql.Append(" SELECT COUNT(*) [count] FROM UT_EventRec AS B   ");
            strSql.Append(" INNER JOIN UT_Equipment AS C ON B.EqID=C.EqID ");
            strSql.Append(" INNER JOIN UT_Line AS M ON C.LineID=M.LineID ");
            strSql.Append(" INNER JOIN UT_EType AS D ON B.ETypeCode=D.ETypeCode ");
            strSql.Append(" INNER JOIN UT_ETypeDetail AS F ON B.DetailID=F.DetailID ");
            strSql.Append(" LEFT JOIN UT_User AS G ON B.StartUserID=G.UserID ");
            strSql.Append(" WHERE B.EndTime IS NULL ");
            if (!string.IsNullOrEmpty(qcmodel.BizDateFrom))
            {
                strSql.Append(" AND B.BizDate >=@SBizDate");
            }
            if (!string.IsNullOrEmpty(qcmodel.BizDateEnd))
            {
                strSql.Append(" AND B.BizDate <=@EBizDate");
            }
            if (!string.IsNullOrEmpty(qcmodel.LineID))
            {
                strSql.Append(" AND M.LineID ='" + qcmodel.LineID + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.EqCode))
            {
                strSql.Append(" AND C.EqCode ='" + qcmodel.EqCode + "'");
            }
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    if (!string.IsNullOrEmpty(qcmodel.BizDateFrom))
                    {
                        Db.AddInParameter(cmd, "@SBizDate", DbType.DateTime, Convert.ToDateTime(qcmodel.BizDateFrom));
                    }
                    if (!string.IsNullOrEmpty(qcmodel.BizDateEnd))
                    {
                        Db.AddInParameter(cmd, "@EBizDate", DbType.DateTime, Convert.ToDateTime(qcmodel.BizDateEnd));
                    }

                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            count = Convert.ToInt32(reader["count"]);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return count;
            }
        }

        public IList<AbnormalEntity> GetUnReliefAbnormalList(AbnormalQcEntity qcmodel)
        {
            var list = new List<AbnormalEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            var start = qcmodel.PageSize * qcmodel.PageIndex + 1;
            var end = qcmodel.PageSize + start - 1;
            strSql.Append(" SELECT T.* FROM( ");
            strSql.Append(" SELECT row_number() over (order by B.StartTime DESC ) AS idx,C.EqCode,C.EqName,M.LineCode,M.LineName,B.StartTime,B.EventID,D.ETypeCode,D.ETypeName,F.DetailCode,F.DetailName,B.StartUserID AS StartUserID,G.FullName as StartUserFullName FROM  UT_EventRec AS B   ");
            strSql.Append(" INNER JOIN UT_Equipment AS C ON B.EqID=C.EqID ");
            strSql.Append(" INNER JOIN UT_Line AS M ON C.LineID=M.LineID ");
            strSql.Append(" INNER JOIN UT_EType AS D ON B.ETypeCode=D.ETypeCode ");
            strSql.Append(" INNER JOIN UT_ETypeDetail AS F ON B.DetailID=F.DetailID ");
            strSql.Append(" LEFT JOIN UT_User AS G ON B.StartUserID=G.UserID ");
            strSql.Append(" WHERE B.EndTime IS NULL ");
            if (!string.IsNullOrEmpty(qcmodel.BizDateFrom))
            {
                var date = Convert.ToDateTime(qcmodel.BizDateFrom);
                strSql.Append(" AND B.BizDate >='" + date + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.BizDateEnd))
            {
                var date = Convert.ToDateTime(qcmodel.BizDateEnd);
                strSql.Append(" AND B.BizDate <='" + date + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.LineID))
            {
                strSql.Append(" AND M.LineID ='" + qcmodel.LineID + "'");
            }
            if (!string.IsNullOrEmpty(qcmodel.EqCode))
            {
                strSql.Append(" AND C.EqCode ='" + qcmodel.EqCode + "'");
            }
            strSql.Append(") AS T ");
            strSql.Append(" Where T.idx between " + start + " and " + end);
            if (qcmodel.SortField != "" && qcmodel.SortOrder != "")
            {
                strSql.Append(" Order by " + qcmodel.SortField + " " + qcmodel.SortOrder);
            }
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {

                            var model = new AbnormalEntity();
                            if (reader["EventID"] != DBNull.Value)
                                model.EventID = new Guid(reader["EventID"].ToString());
                            model.LineCode = reader["LineCode"] == obj ? null : Convert.ToString(reader["LineCode"]);
                            model.LineName = reader["LineName"] == obj ? null : Convert.ToString(reader["LineName"]);
                            model.EqCode = reader["EqCode"] == obj ? null : Convert.ToString(reader["EqCode"]);
                            model.EqName = reader["EqName"] == obj ? null : Convert.ToString(reader["EqName"]);
                            if (reader["StartTime"] != DBNull.Value)
                                model.StartTime = Convert.ToDateTime(reader["StartTime"]);
                            model.ETypeName = reader["ETypeName"] == obj ? null : Convert.ToString(reader["ETypeName"]);
                            model.DetailCode = reader["DetailCode"] == obj ? null : Convert.ToString(reader["DetailCode"]);
                            model.DetailName = reader["DetailName"] == obj ? null : Convert.ToString(reader["DetailName"]);
                            if (reader["StartUserID"] != DBNull.Value)
                                model.StartUserID = new Guid(reader["StartUserID"].ToString());
                            model.StartUserFullName = reader["StartUserFullName"] == obj ? null : Convert.ToString(reader["StartUserFullName"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }

        public bool RemoveUnReliefAbnormal(string id)
        {
            var strSql = new StringBuilder();
            strSql.Append(" Delete From UT_EventRec Where EventID=@EventID ");
            var result = false;
            using (DbCommand cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@EventID", id);
                    result = Db.ExecuteNonQuery(cmd) > 0;
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        public bool ModifyUnreliefAbnormal(UnreliefAbnormalEntity model)
        {

            bool result = false;
            var strSql = new StringBuilder();
            strSql.Append(" UPDATE UT_EventRec SET [EndTime] = @EndTime,[EndUserID] = @EndUserID WHERE EventID = @EventID ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@EndTime", model.EndTime);
                    Db.AddInParameter(cmd, "@EndUserID", model.EndUserID);
                    Db.AddInParameter(cmd, "@EventID", model.EventID);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
    }
}
